package com.serviceprovider.business.module.report.valueobjects;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.sp.transaction.model.Batch;
import com.serviceprovider.business.module.sp.transaction.model.TransactionDetail;
import com.serviceprovider.business.module.sp.transaction.model.TransactionUserAmount;
import com.serviceprovider.business.module.sp.transaction.valueobjects.TransactionDetailsVO;

import org.apache.commons.collections.FactoryUtils;
import org.apache.commons.collections.map.LazyMap;

//~--- JDK imports ------------------------------------------------------------

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BatchVO {
    @SuppressWarnings("unchecked")
    private Map<Integer, TransactionDetailsVO> transactions =
        LazyMap.decorate(new HashMap<Integer, TransactionDetailsVO>(),
                         FactoryUtils.instantiateFactory(TransactionDetailsVO.class));
    private Map<Integer, String> payeeMap = new HashMap<Integer, String>();
    private Date                 batchDate;
    private String               customLabel1Name;
    private String               customLabel2Name;
    private Integer              id;
    private Boolean              includedInOrder;

    public BatchVO() {}

    public BatchVO(Batch batch) {
        payeeMap.clear();
        this.id               = batch.getId();
        this.batchDate        = batch.getBatchDate();
        this.customLabel1Name = batch.getCustomLabelName1();
        this.customLabel2Name = batch.getCustomLabelName2();

        List<TransactionDetail> trList = batch.getTransactionDetails();

        if (trList != null) {
            for (TransactionDetail item : trList) {
                transactions.put(item.getId(), new TransactionDetailsVO(item));

                for (TransactionUserAmount userAmount : item.getTransactionUserAmount()) {
                    payeeMap.put(userAmount.getUserId(),
                                 userAmount.getUser().getFirstName() + " " + userAmount.getUser().getLastName());
                }
            }
        }
    }

    public Map<Integer, TransactionDetailsVO> getTransactions() {
        return transactions;
    }

    public void setTransactions(Map<Integer, TransactionDetailsVO> transactions) {
        this.transactions = transactions;
    }

    public Boolean getIncludedInOrder() {
        return includedInOrder;
    }

    public void setIncludedInOrder(Boolean includedInOrder) {
        this.includedInOrder = includedInOrder;
    }

    public String getCustomLabel1Name() {
        return customLabel1Name;
    }

    public void setCustomLabel1Name(String customLabel1Name) {
        this.customLabel1Name = customLabel1Name;
    }

    public String getCustomLabel2Name() {
        return customLabel2Name;
    }

    public void setCustomLabel2Name(String customLabel2Name) {
        this.customLabel2Name = customLabel2Name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Date getBatchDate() {
        return batchDate;
    }

    public void setBatchDate(Date batchDate) {
        this.batchDate = batchDate;
    }

    public Map<Integer, String> getPayeeMap() {
        return payeeMap;
    }

    public void setPayeeMap(Map<Integer, String> payeeMap) {
        this.payeeMap = payeeMap;
    }

    public Integer getNoOfPayees() {
        return this.payeeMap.size();
    }

    @Override
    public boolean equals(Object obj) {
        if ((obj == null) ||!(obj instanceof BatchVO)) {
            return false;
        }

        BatchVO b = (BatchVO) obj;

        return ((id == null) && (b.id == null)) || ((id != null) && id.equals(b.id));
    }

    @Override
    public int hashCode() {
        return (id != null)
               ? id.hashCode()
               : 0;
    }
}



